$(function()
{
	// ajouter admin
	$('input#name, input#firstname').bind('keyup blur', function() {
		$(this).val($(this).val().toUpperCase()); // Gestion des majuscules pour noms/prénoms
	});
	
	$('a#open_admin_add').click(function() {
		$('div#admin_add').dialog('open');
		$('div.ui-dialog').css('font-size', 11);
	});
	
	$('div#admin_add input')
	.focus(function() {
		$(this).addClass('ui-state-highlight');
	})
	.blur(function() {
		$(this).removeClass('ui-state-highlight');
	});
	
	$('div#admin_add').dialog({
		autoOpen: false,
		modal: true,
		closeOnEscape: true,
		resizable: false,
		width: 400,
		title: 'Créer un admin',
		buttons: {
			Réinitialiser: function() {
				$('div.ajax').html('');
				$('input').val('').removeClass('ui-state-highlight, ui-state-error');
			},
			
			Annuler: function() {
				$('div.ajax').html('');
				$('input').removeClass('ui-state-highlight, ui-state-error');
				$(this).dialog('close');
			},
			
			Enregistrer: function() {
				$('div.ajax').html('<img src="images/ajax-loader-min.gif" alt="Enregistrement en cours..." />');
				$('input').removeClass('ui-state-highlight, ui-state-error');
				
				$('div#admin_add input').each(function() {
					if($(this).val() == '')
						$(this).removeClass('ui-state-highlight').addClass('ui-state-error');
				});
				
				if($('input').hasClass('ui-state-error') || $('select[name=status]').val() == -1) {
					$('div.ajax').html('<p class="error">Tous les champs doivent être remplis</p>');
				}
				else if($('input#mdp2').val() != $('input#mdp1').val()) {
					$('input#mdp2').removeClass('ui-state-highlight').addClass('ui-state-error');
					$('div.ajax').html('<p class="error">Les mots de passe ne correspondent pas</p>');
				}				
				else {
					var name = $('input#name').val(),
						firstname = $('input#firstname').val(),
						mail = $('input#mail').val(),
						mdp = $('input#mdp1').val(),
						status = $('select#status').val(),
						status_name = $('select#status option:selected').html();
					
					$.ajax({
						url: 'pages_admin/ajax_admin.php',
						type: 'GET',
						data: 'action=admin_add'
							+ '&name=' + name
							+ '&firstname=' + firstname
							+ '&mail=' + mail
							+ '&mdp=' + mdp
							+ '&status=' + status,
						
						success: function(result) {
							result = result.split('&&&');
							if(result[0] == 'ok') {
								$('table.table-contain').append('<tr><td>'+name+'</td><td>'+firstname+'</td><td>'+mail+'</td><td class="'+status+'">'+status_name+'</td></tr>');
								$('div.ajax').html('<p class="success">Enregistrement réussi !</p>');
							}
							else
								$('div.ajax').html(result[1]);
						}
					});
				}
			}
		}
	});
	
	
	// supprimer admin
	$('a#open_admin_delete').click(function() {
		$('div#admin_delete').dialog('open');
		$('div.ui-dialog').css('font-size', 11);
	});
	
	$('div#admin_delete').dialog({
		autoOpen: false,
		modal: true,
		closeOnEscape: true,
		resizable: false,
		width: 400,
		title: 'Supprimer un admin',
		buttons: {
			Annuler: function() {
				$('div.ajax').html('');
				$(this).dialog('close');
			},
			
			Supprimer: function() {
				$('div.ajax').html('<img src="images/ajax-loader-min.gif" alt="Enregistrement en cours..." />');
				
				var id = $('select#admins').val(),
					mail = $('select#admins option:selected').html().split(' ')[3];
					
				$.ajax({
					url: 'pages_admin/ajax_admin.php',
					type: 'GET',
					data: 'action=admin_delete'
						+ '&id=' + id,
					
					success: function(result) {
						result = result.split('&&&');
						if(result[0] == 'ok') {
							$("td:contains('"+mail+"')").parent().remove();
							$('select#admins option:selected').remove();
							$('div.ajax').html('<p class="success">Suppression réussie !</p>');
						}
						else
							$('div.ajax').html(result[1]);
					}
				});
			}
		}
	});
	
	
	// modifier statut admin
	$('img.edit_status').click(function() {
		if(!$('select#new_status').is(':visible')) {
			var td =  $(this).parent(),
				id = $(this).attr('id').split('_')[1],
				status = $(td).attr('class'),
				select = $('select#status').clone().attr('id','new_status');
			
			$(td).html(select).append('&nbsp;&nbsp;<input type="submit" id="id_'+id+'" class="admin_update_status" value="OK" />');
			$('select#new_status option').each(function() {
				if($(this).val() == -1)
					$(this).remove();
				if($(this).val() == status)
					$(this).attr('selected', 'selected');
			});
		}
	});
	
	$('input.admin_update_status').live('click', function() {
		var td = $(this).parent();
		
		$(td).append('<span class="right"><img src="images/ajax-loader-min.gif" alt="loading" /></span>');
		
		var action = $(this).attr('class'),
			id = $(this).attr('id').split('_')[1],
			status_name = $('select#new_status option:selected').html(),
			status = $('select#new_status').val();
			
		$.ajax({
			url: 'pages_admin/ajax_admin.php',
			type: 'GET',
			data: 'action=' + action
				+ '&id=' + id
				+ '&status=' + status,
			
			success: function(result) {
				result = result.split('&&&');
				if(result[0] == 'ok') {
					$(td).attr('class', status).html(status_name).append('<span class="right"><img src="images/icons/accept.png" alt="Succès" /></span>');
				}
				else
					$(td).attr('class', 'A').html('Non effectué').append('<span class="right"><img src="images/icons/exclamation.png" alt="Erreur" /></span>');
			}
		});
	});
	
	
	// modifier un paramètre
	function reinit_param_edit_dialog() {
		$('span#param_key').html('');
		$('span#param_type').html('');
		$('textarea#param_value').val('');
		$('div.ajax').html('');
	}
	
	$('img.edit_param').click(function() {
		reinit_param_edit_dialog();
		$('div.ajax').html('<img src="images/ajax-loader-min.gif" alt="Enregistrement en cours..." />');
		$('div#param_edit').dialog('open');
		$('div.ui-dialog').css('font-size', 11);
		// Récupérer les infos du paramètre
		$.getJSON('pages_admin/ajax_admin.php', {action:"param_get_edit_infos", key:$(this).attr('id')}, function(result) {
				$('input#key').val(result.key);
				$('span#param_key').html(result.key);
				$('span#param_type').html(result.type);
				$('textarea#param_value').val(result.value);
				$('div.ajax').html('');
		});
	});
	
	$('div#param_edit').dialog({
		autoOpen: false,
		modal: true,
		closeOnEscape: true,
		resizable: false,
		width: 440,
		title: 'Modifier un paramètre',
		buttons: {
			Annuler: function() {
				reinit_param_edit_dialog();
				$(this).dialog('close');
			},
			
			Valider: function() {
				$('div.ajax').html('<img src="images/ajax-loader-min.gif" alt="Enregistrement en cours..." />');
				
				var value = $('textarea#param_value').val();
				
				$.ajax({
					url: 'pages_admin/ajax_admin.php',
					type: 'POST',
					data: 'action=param_edit'
						+ '&key=' + $('input#key').val()
						+ '&value=' + value,
					
					success: function() {
						$('div.ajax').html('<p class="success">Modification enregistrée !</p>');
					},
					error: function() {
						$('div.ajax').html('<p class="error">Erreur à l\'enregistrement !</p>');
					}
				});
			}
		}
	});
});

